﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Diagnostics;

/// <summary>
/// Tools And Company namespace contains diverse useful functions
/// </summary>
namespace Batbaille.TAC
{	
	static public class Tool
	{		
		public static object Clone(object oObjSrc)
		{
			BinaryFormatter formatter = new BinaryFormatter();
			object oObjDst = null;
			using (MemoryStream stream = new MemoryStream())
			{
				formatter.Serialize(stream, oObjSrc);
				stream.Position = 0;
				oObjDst = formatter.Deserialize(stream);
			}
			return oObjDst;
		}

		public static bool IsPrimitive(object oObj)
		{
			Type oTypeObj = oObj.GetType();
			// IsValueType 
			// IsMarshalByRef

			if( oTypeObj.IsPrimitive || oTypeObj  == typeof(string) || oTypeObj  == typeof(decimal) )
				return true;

			return false;
		}
	}
		
	static public class Message
	{
		public static void Exception(Exception e)
		{			
			StackTrace st = new StackTrace();
			StackFrame sf1 = st.GetFrame(0);
			StackFrame sf2 = st.GetFrame(1);

			System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
			if( sf != null )
			{
				string methodName = sf2.GetMethod().DeclaringType.FullName+"."+sf2.GetMethod().Name;				
				Trace.WriteLine( String.Format("!!!!! EXCEPTION !!!!!\nfunction : {0} :\ndetail:\n{1}\n!!!!!!!!!!!!!!!!!!!", methodName, e.ToString()) );
			}
		}
	}
		
	static public class SerializationBinary
	{
		private static string _strDefaultFilePath;
		public static string DefaultFilePath
		{
			get 
			{
				if (_strDefaultFilePath == "")
				{
					return Environment.CurrentDirectory+"\\UnknownBinaryFile.bat";
				}
				return _strDefaultFilePath; 
			}
			set { _strDefaultFilePath = value; }
		}

		public static void Save<T>(T oToSave, string strFilePath="")
		{
			if( strFilePath == "" )
			{
				strFilePath = DefaultFilePath;
			}

			try
			{
				Stream stream = File.Open(strFilePath, FileMode.Create);

				BinaryFormatter formatter = new BinaryFormatter();

				try
				{
					// On sérialise
					formatter.Serialize(stream, oToSave);
				}
				catch (Exception e)
				{
					Trace.WriteLine("!!!!! Exception Batbaille.TAC.SaveBinary : {0}", e.ToString());
				}	
				finally
				{
					stream.Close();
				}

			}
			catch (Exception e)
			{					
				Trace.WriteLine("!!!!! Exception Batbaille.TAC.SaveBinary : {0}", e.ToString());
			}				
		}

		public static T Load<T>(string strFilePath="")
		{
			if (strFilePath == "")
			{
				strFilePath = DefaultFilePath;
			}

			T oToLoad = default(T);
			BinaryFormatter formatter = new BinaryFormatter();
			try
			{
				FileStream stream = File.Open(strFilePath, FileMode.Open);
				oToLoad = (T)formatter.Deserialize(stream);
				stream.Close();
			}
			catch (Exception e)
			{					
				Trace.WriteLine("!!!!! Exception Batbaille.TAC.LoadBinary : {0}", e.ToString());
			}
			return oToLoad;
		}

		public static void Clear<T>(string strFilePath = "") where T : new() 
		{
			if (strFilePath == "")
			{
				strFilePath = DefaultFilePath;
			}

			T oData = new T();
			Save<T>(oData, strFilePath);
		}

		public static void DeleteBinary(string strFilePath = "")
		{
			if (strFilePath == "")
			{
				strFilePath = DefaultFilePath;
			}

			try
			{
				File.Delete(strFilePath);
			}
			catch (Exception e)
			{
				Trace.WriteLine("!!!!! Exception Batbaille.TAC.DeleteBinary : {0}", e.ToString());
			}
		}
	}
}
		/*
		class SerializableData<T> where T : new()
		{
			string m_strUrlFile;
			public string strUrlFile
			{
				get { return m_strUrlFile; }
				set { m_strUrlFile = value; }
			}

			public SerializableData(string strUrlFile)
			{
				m_strUrlFile = strUrlFile;
			}

			public void SaveBinaire(T oToSave)
			{

				Stream stream = File.Open(m_strUrlFile, FileMode.Create);
				BinaryFormatter formatter = new BinaryFormatter();

				// On sérialise
				formatter.Serialize(stream, oToSave);

				stream.Close();
			}

			public T LoadBinaire()
			{
				T oToLoad = default(T);
				BinaryFormatter formatter = new BinaryFormatter();
				try
				{
					FileStream stream = File.Open(m_strUrlFile, FileMode.Open);
					oToLoad = (T)formatter.Deserialize(stream);
					stream.Close();
				}
				catch (FileNotFoundException)
				{

				}
				return oToLoad;
			}

			public void ClearBinaire(T oData)
			{
				oData = new T();
				SaveBinaire(oData);
			}
		}
		 * */	
